package Main;

public class Main {
}

class Solution {
    public static void main(String[] args) {
//        System.out.println(numDistinct("rabbbit", "rabbit"));
    }

    public int numDistinct(String s, String t) {
        int[][] dp = new int[s.length() + 1][t.length() + 1];
        for (int i = 0; i < s.length(); i++) {
            dp[i][0] = 1;
        }
        for (int i = 1; i <= s.length(); i++) {
            for (int j = 1; j <= t.length(); j++) {
                dp[i][j] = dp[i - 1][j];
                if (s.charAt(i-1) == t.charAt(j-1)) {
                    dp[i][j] += dp[i-1][j-1];
                }
            }
        }

        return dp[s.length()][t.length()];
    }

    public static int numDistinct1(String s, String t) {
        return f1(s, 0, t, 0);
    }

    private static int f1(String s, int s1, String t, int t1) {
        if (s1 >= s.length() - 1 || t1 >= t.length() - 1) {
            return 1;
        }
        int ans = 0;
        if (s.charAt(s1) == t.charAt(t1)) {
            ans += (f1(s, s1 + 1, t, t1 + 1) + 1);
        } else {
            ans += f1(s, s1 + 1, t, t1);
            ans += f1(s, s1, t, t1 + 1);
        }

        return ans;
    }
}